package com.example.zte.myapplication;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.zte.myapplication.entity.Root;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    String resultData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 启动登录
        Button btnLogin = (Button)findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
        // 启动用户登录
        Button btnUserLogin = (Button)findViewById(R.id.btn_user_login);
        btnUserLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(MainActivity.this,UserLoginActivity.class);
//                startActivity(intent);
                String [] ss = {"你好","南呈"};
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("这是移动班")
                        .setIcon(R.mipmap.h5)
                        //.setMessage("今天是周四，对吗？")
                        .setView(R.layout.calculator_table)
                       // .setItems(ss,null)
                        //.setMultiChoiceItems(ss,null,null)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                Toast.makeText(MainActivity.this,"对", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("Error", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"错误", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });

        // 启动LIST例子
        Button btnListDemo = (Button)findViewById(R.id.btn_list_demo);
        btnListDemo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(MainActivity.this,ListSimpleActivity.class);
//                startActivity(intent);
                // 创建线程获取网络数据
                new Thread(){
                    public void run(){
                       resultData = "";
                        try {
                            URL url = new URL("http://www.weather.com.cn/data/cityinfo/101280105.html");
                            HttpURLConnection http = (HttpURLConnection) url.openConnection();
                            int state = http.getResponseCode();
                            if(state == HttpURLConnection.HTTP_OK){
                                InputStreamReader in = new InputStreamReader(http.getInputStream());
                                BufferedReader buffer = new BufferedReader(in);
                                String inputLine = null;
                                while(((inputLine = buffer.readLine())!=null)){
                                    resultData+=inputLine;
                                }
                                in.close();
                            }
                            http.disconnect();

                            // 使用UI线程显示文本
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {


                                    Gson gson = new Gson();
                                  Root root = gson.fromJson(resultData, Root.class);
                                    System.out.println(root);
                                    new AlertDialog.Builder(MainActivity.this)
                                            .setMessage(resultData)
                                            .create()
                                            .show();
                                }
                            });

                        }
                        catch (Exception e){
                            System.out.println(e);
                        }
                    }
                }.start();
            }
        });

        // 启动LIST例子
        Button btnLanguage = (Button)findViewById(R.id.button);
        btnLanguage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,LanguageActivity.class);
                startActivity(intent);
            }
        });
    }
}
